package tc.de.krawallmann.robobridge.devices.implementation;

import tc.de.krawallmann.robobridge.communication.Command;
import tc.de.krawallmann.robobridge.communication.CommandType;
import tc.de.krawallmann.robobridge.communication.lan.wlan.RSSIListener;
import tc.de.krawallmann.robobridge.devices.AbstractDevice;
import tc.de.krawallmann.robobridge.devices.Robot;
import tc.de.krawallmann.robobridge.util.MathUtil;

public class M256 extends AbstractDevice implements RSSIListener{

	public static final String M256_KEY = "M256_KEY";
	public static final String RSSI = "RSSI";
	public static final String M256_LEDS = "M256_LEDS";
	public static final int SET_LED_COMMAND = 1;

	private int leds;
	private int rssi;
	public int getPressedKey() {
		return this.pressedKey;
	}

	private int pressedKey;


	public int getRssi() {
		return this.rssi;
	}

	public int getLeds() {
		return this.leds;
	}

	public void setLeds(int leds) {
		int old = this.leds;
		this.leds = leds;
		firePropertyChange(M256_LEDS, old, leds);
		getRobot().queueOutgoingCommand(
				new Command(CommandType.UPDATE_ROBOT_DEVICE, false, getDeviceId(), SET_LED_COMMAND,
						leds));
	}

	

	public M256(Robot robot, int deviceId) {
		super(robot,AbstractDevice.DEVICE_ID_RP6_M256, deviceId, "Arexx", "RP6 M256 WIFI");
	}

	@Override
	public boolean onCommandReceived(Command command) {
		if(containsRelevantData(command, Integer.class, Integer.class)){
			if((Integer)command.getParameter(1)==1){
				int old=leds;
				leds=(Integer)command.getParameter(2);
				firePropertyChange(M256_LEDS, old, leds);
			}else if((Integer)command.getParameter(1)==2){
				int old=pressedKey;
				pressedKey=(Integer)command.getParameter(2);
				firePropertyChange(M256_KEY, old, pressedKey);
			}
			return true;
		}
		return false;
	}

	@Override
	public void onRSSIUpdated(int rssi) {
		int old=this.rssi;
		this.rssi=rssi;
		firePropertyChange(RSSI, old, rssi);
	}

}
